const mongoose = require('mongoose');
const Schema = mongoose.Schema;  // 设计集合(表)结构


// 1. 连接数据库，见/utils/connect.js


// 2. 设计文档(表)结构
// 设计用户表的表结构
const usersSchema = new Schema({
    username: {
        type: String,
        required: true
    },
    phone: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    time: {
        type: Date,
        required: true
    },
}, { versionKey: false })
// { versionKey: false } 往数据表中插入数据时会自动生成一个‘__v’的字段，据说是用来记录版本号，在 Schema 中加入 { versionKey: false } 可以解决这个问题

const moviesSchema = new Schema({
    genres: Array,
    casts: Array,
    directors: Array,
    rating: Object,
    title: String,
    collect_count: Number,
    original_title: String,
    subtype: String,
    year: String,
    images: Object,
    alt: String,
    id: String,
}, { versionKey: false })

const commentSchema = new Schema({
    title: String,
    content: String,
    time: Date,
    author: String,
    movie: Object,
    mid: String,
}, { versionKey: false })


// 3. 将文档结构发布为模型
/**
 *  mongoose.model(params1, params2)  将一个架构发布为 model
 *  params1: 传入一个字符串用来表示你的数据库名称，mongoose会自动将字符串改为 小写复数形式的单词
 *           例如，写 User 会被改为 users
 *  params2: 传入一个架构 Schema
 *  返回值: 模型构造函数
 */
const UsersModel = mongoose.model('users', usersSchema)
const MoviesModel = mongoose.model('movies', moviesSchema)
const CommentModel = mongoose.model('comments', commentSchema)


// 4. 有了模型构造函数之后，就可以使用这个模型构造函数对数据集合(数据表)中的数据进行操作(增删改查)
exports.UsersModel = UsersModel;
exports.MoviesModel = MoviesModel;
exports.CommentModel = CommentModel;



/** ------------------------------------------ 卖座电影接口 ------------------------------------------ */
// 用户接口
const maizuoUsersSchema = new Schema({
    username: {
        type: String,
        required: true
    },
    phone: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    time: Date,
    avatar: String
}, { versionKey: false })

exports.MaizuoUsersModel = mongoose.model('maizuo_users', maizuoUsersSchema)
